layui.define(['admin','table','form','layarea','element'],function (exports){
    let $ = layui.$
        ,admin  = layui.admin
        ,view   = layui.view
        ,table = layui.table
        ,form = layui.form
        ,layarea = layui.layarea
        ,element = layui.element

    let model = 'user'
        ,module = ['list']

    const active = {

        list_form:function (d) {
            admin.popup({
                title: '用户编辑'
                ,area: ['500px', '500px']
                ,id: 'home-machine-table'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[0]+'_form', d).done(function(){

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'user/list/post'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('LAY-user-list'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        }
    }

    let table_id_0 = model+'-'+module[0]
        ,table_path_0 = model+'/'+module[0]

    table.render({
        elem: '#LAY-'+table_id_0
        ,toolbar: '#table-tool'
        ,url: '/api/admins/'+table_path_0
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'name', width:80, title: '名称'}
            ,{field: 'mobile',width: 170, title: '手机号'}
            ,{field: 'identity',width: 200, title: '身份证'}
            ,{field: 'address',title: '地址'}
            ,{field: 'invite_name',width: 100,title: '邀请人'}
            ,{field: 'is_admin',width: 120,title: '管理员',templet:'#table-switch'}
            ,{title: '操作', width: 80, align: 'center', fixed: 'right', toolbar: '#app-table-oper'}
        ]]
        ,page:true
    });

    //监听搜索
    form.on('submit(table-'+table_id_0+'-search)', function(data){
        var field = data.field;

        //执行重载
        table.reload('LAY-'+table_id_0, {
            where: field
        });
    });

    //头部工具栏
    table.on('toolbar(LAY-'+table_id_0+')', function(obj){

        switch(obj.event){
            case 'add':
                active.list_form();
                break;
        };
    });

    table.on('tool(LAY-'+table_id_0+')', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){

        } else if(obj.event === 'edit'){
            active.list_form(data);
        }
    });

    //监听状态工具栏
    form.on('switch(table-'+table_id_0+'-status)', function(obj){
        let data = JSON.parse(decodeURIComponent($(this).data('json')));

        admin.req({
            url: table_path_0+'/admin'
            ,data: {id:data.id,status:obj.elem.checked ?1:0}
        });
    });

    exports('member',active);
});
